package com.gm.manager.module.manager.controller;


import com.gm.manager.common.domian.PE;
import com.gm.manager.common.domian.RP;
import com.gm.manager.common.domian.RS;
import com.gm.manager.common.domian.RSD;
import com.gm.manager.common.entity.db.UserDaoEntity;
import com.gm.manager.common.exception.ParameterException;
import com.gm.manager.common.tools.LoginUtils;
import com.gm.manager.common.tools.ValidatorUtils;
import com.gm.manager.module.login.entity.LoginBack;
import com.gm.manager.module.manager.entity.UserInfo;
import com.gm.manager.module.manager.service.UserInfoService;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@Validated
public class ManagerController {


    private UserInfoService userInfoService;

    @Autowired
    public void setUserInfoService(UserInfoService userInfoService) {
        this.userInfoService = userInfoService;
    }

    @ApiOperation("列表信息")
    @GetMapping("/userInfo")
    public RP userInfo(@NotBlank(message = "请先登录") @RequestParam String token) {
        LoginUtils.checkLogin(token);
        List<UserInfo> all = userInfoService.findAll();
        return new RSD(all);
    }

    @ApiOperation("添加人员")
    @PostMapping("/addUser")
    public RS addUser(@NotBlank(message = "请先登录") @RequestParam String token, @RequestBody UserDaoEntity userDaoEntity) {
        LoginUtils.checkLogin(token);
        userDaoEntity.setPassword(LoginUtils.md5(userDaoEntity.getPassword(),userDaoEntity.getUserName()));
        ValidatorUtils.validateEntity(userDaoEntity);
        userInfoService.saveUser(userDaoEntity);
        return new RS();
    }

    @ApiOperation("修改人员")
    @PostMapping("/updateUser")
    public RS updateUser(@NotBlank(message = "请先登录") @RequestParam String token, @RequestBody UserInfo userInfo) {
        LoginUtils.checkLogin(token);
        if(userInfo.getId()==null){
            throw new ParameterException("用户id缺失");
        }
        UserDaoEntity userDaoEntity=userInfoService.findUserById(userInfo.getId());
        userDaoEntity.setActorId(userInfo.getActorId()==null ? userDaoEntity.getActorId() : Integer.valueOf(userInfo.getActorId()));
        userDaoEntity.setDepartmentId(userInfo.getDepartmentId()==null ?
                userDaoEntity.getDepartmentId() : Integer.valueOf(userInfo.getDepartmentId()));
        userDaoEntity.setTelNumber(userInfo.getTelNumber()==null ?
                userDaoEntity.getTelNumber() : userInfo.getTelNumber());
        userInfoService.updataUser(userDaoEntity);
        return new RS();
    }

    @ApiOperation("删除人员")
    @PostMapping("/deleteUser")
    public RS deleteUser(@NotBlank(message = "请先登录") @RequestParam String token, @NotBlank(message = "请输入删除的id") @RequestParam String id) {
        LoginUtils.checkLogin(token);
        userInfoService.deleteUser(Integer.parseInt(id.trim()));
        return new RS();
    }
}
